package window.dialog;

import manage.DBManager;
import manage.UsernameController;
import window.service.mouseService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.SQLException;

import static manage.LoginManager.login;


public class LoginDialog extends JFrame {
    public LoginDialog(JLabel label) {
        mouseService mouse = new mouseService();
        this.setLayout(new BorderLayout());
        JPanel userPanel = new JPanel();
        JLabel usernameL = new JLabel("Username:");
        ImageIcon icon0 = new ImageIcon("res/icon/username.png");
        icon0.setImage(icon0.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH));
        JLabel userNameLabel = new JLabel(icon0);
        userPanel.add(userNameLabel, BorderLayout.WEST);
        userPanel.add(usernameL, BorderLayout.WEST);
        JTextField userT = new JTextField(12);
        userPanel.add(userT);
        userPanel.setBorder(BorderFactory.createEmptyBorder(25, 0, 0, 0));
        this.add(userPanel, BorderLayout.NORTH);

        JPanel passwordPanel = new JPanel();
        JLabel passwordL = new JLabel("Password: ");
        ImageIcon icon1 = new ImageIcon("res/icon/password.png");
        icon1.setImage(icon1.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH));
        JLabel passwordLabel = new JLabel(icon1);
        passwordPanel.add(passwordLabel, BorderLayout.WEST);
        passwordPanel.add(passwordL, BorderLayout.WEST);
        JPasswordField passwordT = new JPasswordField(12);
        passwordPanel.add(passwordT);
        this.add(passwordPanel, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 0, 20, 0));
        JButton LoginButton = new JButton("Login");
        mouse.ButtonChangeHand(LoginButton);
        buttonPanel.add(LoginButton);
        LoginButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                dispose();
            }
        });

        LoginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = userT.getText();
                String password = String.valueOf(passwordT.getPassword());
                if (login(username, password)) {
                    JOptionPane.showMessageDialog(LoginDialog.this, "Login Successful!");
                    UsernameController.setUsername(username);
                    label.setText(UsernameController.getUsername());
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(LoginDialog.this, "Login Failed! Please check your username and password", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        });


        this.add(buttonPanel, BorderLayout.SOUTH);


        //设置窗口风格
        setTitle("Login");
        setSize(300, 200);
        setVisible(true);
        setLocationRelativeTo(null);
    }
}

